import { Tooltip } from 'antd';
import { PlayCircleOutlined, EyeOutlined, EditOutlined } from '@ant-design/icons';
import { getColorTextLoadMap, removeDateHmsStr, tooltipLength } from '@/utils';
import pageStyles from '@/common/pageLayout.less';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { ManageCommonAPI } from '@/pages/type';
import MyIcon from '@/components/MyIcon';

// 操作按钮
const Operation = (curRes: Record<string, any>, operateFuns: DelegateManageAPI.IOperationClick, record: Record<string, any>) => {
  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EyeOutlined />,
      btnName: '查看',
      clickFnc: operateFuns,
      clickType: 'onView',
      isVisible: curRes?.['/delegateController/getMactivitiDelegateByID'] && record?.id,
    },
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible: curRes?.['/delegateController/edit'] && record?.id && record?.isOpen == '1',
    },
    {
      icon: <PlayCircleOutlined />,
      btnName: <span>{record?.id && record?.isOpen == '1' ? '禁用' : record?.isOpen == '0' ? '启用' : '启用'}</span>,
      clickFnc: operateFuns,
      clickType: 'onSwitch',
      isVisible: curRes['/delegateController/setSwitch'] && record?.id,
    },
  ];

  return tableMoreBtnConfig(otherBtnConfig, record);
};

const getTableColumns = (curRes: Record<string, any>, operateFuns: DelegateManageAPI.IOperationClick) => {
  const { onView } = operateFuns;
  return [
    {
      title: '业务名称',
      dataIndex: 'categoryName',
      key: 'categoryName',
      width: 150,
      visible: true,
      fixed: 'left',
      unDrag: true,
      sorter: (a: any, b: any) => (a?.category > b?.category ? 1 : -1),
      render: (text: string | null) => {
        const { color, colorText } = getColorTextLoadMap(text);
        return (
          <span className={pageStyles['over-flow-hidden']} style={{ color }}>
            {colorText}
          </span>
        );
      },
    },
    {
      title: '委托原因',
      dataIndex: 'delegateReason',
      key: 'delegateReason',
      width: 180,
      visible: true,
      render: (text: string | null, record: Record<string, any>) => {
        return (
          <a
            onClick={() => {
              onView(record);
            }}
            style={{ color: '100%' }}
          >
            <Tooltip title={text ? tooltipLength(text, 100) : ''}>
              <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },
    {
      title: '委托状态',
      dataIndex: 'isOpen',
      key: 'isOpen',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => (a?.isOpen > b?.isOpen ? 1 : -1),
      render: (text: number | null) => {
        let color;
        let colorText;
        if (text == 1) {
          (color = '#59d6a5'), (colorText = '有效');
        } else if (text == 2) {
          (color = '#5c6f91'), (colorText = '作废');
        } else if (text == 0) {
          (color = 'red'), (colorText = '禁用');
        }
        return (
          <span className={pageStyles['over-flow-hidden']}>
            <MyIcon type="icon-dian" style={{ color }} />
            {colorText}
          </span>
        );
      },
    },
    {
      title: '委托开始时间',
      dataIndex: 'startTime',
      key: 'startTime',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a.startTime > b.startTime ? 1 : -1),
      render: (text: string) => {
        const dateText: string = removeDateHmsStr(text);
        return <span className={pageStyles['over-flow-hidden']}>{dateText}</span>;
      },
    },
    {
      title: '委托结束时间',
      dataIndex: 'endTime',
      key: 'endTime',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a.endTime > b.endTime ? 1 : -1),
      render: (text: string) => {
        const dateText: string = removeDateHmsStr(text);
        return <span className={pageStyles['over-flow-hidden']}>{dateText}</span>;
      },
    },
    {
      title: '委托人',
      dataIndex: 'assignerName',
      key: 'assignerName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a.assignerID > b.assignerID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '委托岗位',
      dataIndex: 'assignPositionName',
      key: 'assignPositionName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a.assignPositionID > b.assignPositionID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '委托部门',
      dataIndex: 'assignDepartmentName',
      key: 'assignDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.assignDepartmentID > b.assignDepartmentID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '委托公司',
      dataIndex: 'assignCompanyName',
      key: 'assignCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.assignCompanyID > b.assignCompanyID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '代理人',
      dataIndex: 'agenterName',
      key: 'agenterName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a.agenterID > b.agenterID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '代理岗位',
      dataIndex: 'agentPositionName',
      key: 'agentPositionName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a.agentPositionID > b.agentPositionID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '代理部门',
      dataIndex: 'agentDepartmentName',
      key: 'agentDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.agentDepartmentID > b.agentDepartmentID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '代理公司',
      dataIndex: 'agentCompanyName',
      key: 'agentCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.agentCompanyID > b.agentCompanyID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请时间',
      dataIndex: 'createTime',
      key: 'createTime',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.createTime > b.createTime ? 1 : -1),
      render: (text: string) => {
        const dateText: string = removeDateHmsStr(text);
        return <span className={pageStyles['over-flow-hidden']}>{dateText}</span>;
      },
    },
    {
      title: '申请人',
      dataIndex: 'createrName',
      key: 'createrName',
      visible: false,
      width: 120,
      sorter: (a: any, b: any) => (a.createrID > b.createrID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.createPositionID > b.createPositionID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.createDepartmentID > b.createDepartmentID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请公司',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.createCompanyID > b.createCompanyID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.modifyTime > b.modifyTime ? 1 : -1),
      render: (text: string) => {
        const dateText: string = removeDateHmsStr(text);
        return <span className={pageStyles['over-flow-hidden']}>{dateText}</span>;
      },
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      visible: false,
      width: 120,
      sorter: (a: any, b: any) => (a.modifyerID > b.modifyerID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '修改岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.modifyPositionID > b.modifyPositionID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '修改部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.modifyDepartmentID > b.modifyDepartmentID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '修改公司',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.modifyCompanyID > b.modifyCompanyID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '备注',
      dataIndex: 'remark',
      key: 'remark',
      visible: false,
      width: 220,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 220,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (text: any, record: any) => {
        return (
          <div
            style={{
              display: 'flex',
            }}
          >
            {Operation(curRes, operateFuns, record)}
          </div>
        );
      },
    },
  ];
};

export { getTableColumns };
